package com.zhuobowen.warehouse.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.zhuobowen.common.idempotent.annotation.RepeatSubmit;
import com.zhuobowen.common.log.annotation.Log;
import com.zhuobowen.common.web.core.BaseController;
import com.zhuobowen.common.mybatis.core.page.PageQuery;
import com.zhuobowen.common.core.domain.R;
import com.zhuobowen.common.core.validate.AddGroup;
import com.zhuobowen.common.core.validate.EditGroup;
import com.zhuobowen.common.log.enums.BusinessType;
import com.zhuobowen.common.excel.utils.ExcelUtil;
import com.zhuobowen.warehouse.domain.vo.ExportBillVo;
import com.zhuobowen.warehouse.domain.bo.ExportBillBo;
import com.zhuobowen.warehouse.service.IExportBillService;
import com.zhuobowen.common.mybatis.core.page.TableDataInfo;

/**
 * 出库单
 *
 * @author zbw
 * @date 2025-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/exportBill")
public class ExportBillController extends BaseController {

    private final IExportBillService exportBillService;

    /**
     * 查询出库单列表
     */
    @SaCheckPermission("warehouse:exportBill:list")
    @GetMapping("/list")
    public TableDataInfo<ExportBillVo> list(ExportBillBo bo, PageQuery pageQuery) {
        return exportBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出出库单列表
     */
    @SaCheckPermission("warehouse:exportBill:export")
    @Log(title = "出库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExportBillBo bo, HttpServletResponse response) {
        List<ExportBillVo> list = exportBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "出库单", ExportBillVo.class, response);
    }

    /**
     * 获取出库单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:exportBill:query")
    @GetMapping("/{id}")
    public R<ExportBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(exportBillService.queryById(id));
    }

    /**
     * 新增出库单
     */
    @SaCheckPermission("warehouse:exportBill:add")
    @Log(title = "出库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExportBillBo bo) {
        return toAjax(exportBillService.insertByBo(bo));
    }

    /**
     * 修改出库单
     */
    @SaCheckPermission("warehouse:exportBill:edit")
    @Log(title = "出库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExportBillBo bo) {
        return toAjax(exportBillService.updateByBo(bo));
    }

    /**
     * 删除出库单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("warehouse:exportBill:remove")
    @Log(title = "出库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(exportBillService.deleteWithValidByIds(List.of(ids), true));
    }
}
